
include "../lib/scf_rbtree.c";
include "../lib/scf_capi.c";

struct rbtree_test_t
{
	scf_rbtree_node_t  node;
	int d;
};

static int test_cmp(scf_rbtree_node_t* node0, void* data)
{
	rbtree_test_t* v0 = (rbtree_test_t*)node0;
	rbtree_test_t* v1 = (rbtree_test_t*)data;

	if (v0->d < v1->d)
		return -1;
	else if (v0->d > v1->d)
		return 1;
	return 0;
}

static int test_find(scf_rbtree_node_t* node0, void* data)
{
	rbtree_test_t* v0 = (rbtree_test_t*)node0;
	int            d1 = (intptr_t)data;

	if (v0->d < d1)
		return -1;
	else if (v0->d > d1)
		return 1;
	return 0;
}

static int test_print(scf_rbtree_node_t* node0, void* data)
{
	rbtree_test_t* v0 = (rbtree_test_t*)node0;

	printf("v0->d: %d\n", v0->d);
	return 0;
}

const int N = 17;

int main()
{
	scf_rbtree_t  tree;
	scf_rbtree_init(&tree);

	rbtree_test_t* d;

	int i;
	for (i = 0; i < N; i++) {
		d = calloc(1, sizeof(rbtree_test_t));

		d->d = i;

		int ret = scf_rbtree_insert(&tree, &d->node, test_cmp);
		if (ret < 0)
			return -1;
	}

	scf_rbtree_foreach(&tree, tree->root, NULL, test_print);

	scf_rbtree_depth(&tree, tree->root);

	for (i = 0; i < N / 2; i++) {
		d = (rbtree_test_t*) scf_rbtree_find(&tree, (void*)(intptr_t)i, test_find);

		int ret = scf_rbtree_delete(&tree, &d->node);

		free(d);
		d = NULL;
	}

	scf_rbtree_foreach(&tree, tree->root, NULL, test_print);

	scf_rbtree_depth(&tree, tree->root);

	for (i = 0; i < N / 2; i++) {
		d = calloc(1, sizeof(rbtree_test_t));

		d->d = i;

		int ret = scf_rbtree_insert(&tree, &d->node, test_cmp);
		if (ret < 0)
			return -1;
	}
	scf_rbtree_foreach_reverse(&tree, tree->root, NULL, test_print);

	scf_rbtree_depth(&tree, tree->root);
	return 0;
}


